package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.firecommand.dto.PlotDetailsDto;
import com.telit.firecommand.dto.PlotDto;
import com.telit.firecommand.dto.PlotReturnDto;
import com.telit.firecommand.entity.CadPlot;
import com.telit.firecommand.service.ICadPlotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 联动标绘 前端控制器
 * </p>
 *
 * @author yummy
 * @since 2021-07-31
 */
@RestController
@RequestMapping("/cad-plot")
@Api(tags = "联动标绘", description = "/cad-plot")
public class CadPlotController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ICadPlotService cadPlotService;

    @PostMapping("/add")
    @ApiOperation("添加")
    public DataReturn add(@RequestBody PlotDto plotDto) {
        Long value = cadPlotService.add(plotDto);
        return DataReturn.ok(value);
    }

    @GetMapping("/delete/{id}")
    @ApiImplicitParam(name = "id", value = "配置ID", paramType = "path", dataType = "Integer")
    public DataReturn delete(@PathVariable("id") Integer id) {
        int value = cadPlotService.delete(id);
        return DataReturn.ok(value);
    }

    @GetMapping("/deleteBatch")
    @ApiImplicitParam(name = "ids", value = "配置ID数组", paramType = "query", dataType = "integer[]")
    public DataReturn deleteBatch(Integer[] ids) {
        List<Integer> values = cadPlotService.deleteBatch(ids);
        return DataReturn.ok(values);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public DataReturn update(@RequestBody CadPlot cadPlot) {
        cadPlotService.updateData(cadPlot);
        return DataReturn.ok();
    }

    @PostMapping("/search")
    @ApiOperation("条件查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页面", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页记录数", dataType = "int", paramType = "query")})
    public DataReturn findListByPage(@RequestBody(required = false) CadPlot cadPlot, Integer page,
                                     Integer pageCount) {
        IPage<CadPlot> cadPlotPage = cadPlotService.findListByPage(cadPlot, page, pageCount);
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("cadPlot", cadPlotPage.getRecords());
        retMap.put("total", cadPlotPage.getTotal());
        return DataReturn.ok(retMap);
    }

    @PostMapping("/searchNoPage")
    @ApiOperation("条件查询")
    public DataReturn searchNoPage(@RequestBody(required = false) CadPlot cadPlot) {
        List<CadPlot> cadPlotList = cadPlotService.searchNoPage(cadPlot);
        return DataReturn.ok(cadPlotList);
    }

    @GetMapping("/searchByFireAlarmId/{fireAlarmId}")
    @ApiOperation("根据火情ID查询联动标绘")
    @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", dataType = "String", paramType = "path")
    public DataReturn searchByFireAlarmId(@PathVariable("fireAlarmId") String fireAlarmId) {
        List<PlotReturnDto> plotReturnDtoList = cadPlotService.searchByFireAlarmId(fireAlarmId);
        return DataReturn.ok(plotReturnDtoList);
    }

    @GetMapping("/searchDetailsByFireAlarmId/{fireAlarmId}")
    @ApiOperation("根据火情ID查询联动标绘地图信息")
    @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", dataType = "String", paramType = "path")
    public DataReturn searchDetailsByFireAlarmId(@PathVariable("fireAlarmId") String fireAlarmId) {
        List<PlotDetailsDto> plotDetailsDtoList = cadPlotService.searchDetailsByFireAlarmId(fireAlarmId);
        return DataReturn.ok(plotDetailsDtoList);
    }

    @GetMapping("/searchAllDetailsByFireAlarmId/{fireAlarmId}")
    @ApiOperation("根据火情ID查询联动标绘地图信息(全部）")
    @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", dataType = "String", paramType = "path")
    public DataReturn searchAllDetailsByFireAlarmId(@PathVariable("fireAlarmId") String fireAlarmId) {
        List<PlotDetailsDto> plotDetailsDtoList = cadPlotService.searchAllDetailsByFireAlarmId(fireAlarmId);
        return DataReturn.ok(plotDetailsDtoList);
    }

    @GetMapping("/findOne/{id}")
    @ApiOperation("根据id获取配置实体")
    @ApiImplicitParam(name = "id", value = "配置ID", paramType = "path", dataType = "Long")
    public DataReturn findById(@PathVariable Integer id) {
        CadPlot cadPlot = cadPlotService.findById(id);
        return DataReturn.ok(cadPlot);
    }

}
